package code;

import EXtension.FrameWork.EventSystem;
import EXtension.FrameWork.MyDelegate;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.control.ScrollPane;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;

import java.io.File;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThumbnailsArea extends ScrollPane {

    public List<MyThumbnail> selectedThumbnails=new ArrayList<>();
    public FlowPane flowPane=new FlowPane();
    public Task<Integer> task=new Task<Integer>() {
        @Override
        protected Integer call() throws Exception {
            return null;
        }
    };
    public ExecutorService executor= Executors.newCachedThreadPool();
    private int dirLength;

    public ThumbnailsArea() {

        register();
        /**
         * 当 setFitToHeight(true) 被调用时，在 ScrollPane 高度改变时，
         * 垂直滚动条将被添加或移除,以确保内部内容可以完全显示。
         * 如果设置为 false，则即使高度发生变化也不会调整垂直滚动条，并且用户需要通过滚动来查看超出容器尺寸的部分。
         * 注意：在使用 setFitToHeight() 方法之前，
         * 您需要先将 ScrollPane 的内容设置为其自己的首选大小。
         * 这可以通过将 ScrollPane 的 content 设置为具有固定首选大小的节点来完成。
         */
        this.setContent(flowPane);
        flowPane.setHgap(10);
        flowPane.setVgap(10);
        flowPane.prefHeightProperty().bind(this.heightProperty());
        flowPane.prefWidthProperty().bind(this.widthProperty());
        this.setPrefWidth(160);
        this.setPrefHeight(160);
        this.setFitToHeight(true);
        this.setFitToHeight(true);


    }
    public void loadThumbnails(File[] dir){
        dirLength=dir.length;
        flowPane.getChildren().clear();
        if(task.isRunning()){
            task.cancel();
        }
        newTask(dir);
        executor.execute(task);
    }
    private void newTask(File[] dir){
        task=new Task<Integer>() {
            @Override
            protected Integer call() throws Exception {
                for (File f :
                        dir) {
                    if(task.isCancelled()){
                        flowPane.getChildren().clear();
                        break;
                    }
                    else {
                        MyThumbnail myThumbnail=new MyThumbnail(f);
                        updateMessage("已加载："+flowPane.getChildren().size());

                        Platform.runLater(new Task<Integer>() {
                            @Override
                            protected Integer call() throws Exception {
                                if(this.isCancelled()){
                                    return 0;
                                }
                                flowPane.getChildren().add(myThumbnail);
                                return flowPane.getChildren().size();
                            }
                        });
                    }
                }
                return null;
            }
        };
    }

    public List<Node> getThumbnails(){
        return flowPane.getChildren();
    }
    private void register(){
        EventSystem eventSystem=EventSystem.instance;
        eventSystem.register(new MyDelegate("SingleChoose") {

            @Override
            public void execute(Object data) {

                for (Node n :
                        flowPane.getChildren()) {
                    MyThumbnail my = (MyThumbnail) n;
                    my.isSelected.set(false);
                }
                MyThumbnail my=(MyThumbnail) data;
                //System.out.println(my.isFocused());
                my.isSelected.set(true);
            }

        });
        flowPane.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                flowPane.requestFocus();
                System.out.println("FFF"+flowPane.isFocused());
            }
        });
    }
}
